package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.SearchRecommendInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 搜索主页搜索推荐数据解析
 * 
 * @author linyuren
 * 
 */
public class BookSearchParser extends HandlerBase {

    private SearchRecommendInfo seaRecInfo;
    private List<BookInfo> booksList;// 书籍的集合
    private List<BookInfo> labelList;// 标签的集合
    private BookInfo info;
    private int inWhich;
    private final int BOOKNAME = 1;
    private final int AUTHOR = 2;
    private final int BOOKTYPE = 3;
    private final int BOOKDESCRIPTION = 4;

    @Override
    public SearchRecommendInfo getParsedData() {
        return seaRecInfo;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            switch (this.inWhich) {
                case BOOKNAME:
                    this.info.setBookName((info.getBookName() == null ? "" : info.getBookName())
                            + value);
                    break;
                case AUTHOR:
                    this.info.setBookAuthorName((info.getBookAuthorName() == null ? "" : info.getBookAuthorName()) + value);
                    break;
                case BOOKTYPE:
                    this.info.setBookType((info.getBookType() == null ? "" : info.getBookType())
                            + value);
                    break;
                case BOOKDESCRIPTION:
                    this.info.setBookIntro((info.getBookIntro() == null ? "" : info.getBookIntro()) + value);
                    break;
            }
        }
    }

    @Override
    public void startDocument() throws SAXException {
        seaRecInfo = new SearchRecommendInfo();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs)
            throws SAXException {
        if (localName.equals("Books")) {
            booksList = new ArrayList<BookInfo>();
            seaRecInfo.setBooksList(booksList);
        } else if (localName.equals("KeyWords")) {
            labelList = new ArrayList<BookInfo>();
            seaRecInfo.setLabelList(labelList);
        } else if (localName.equals("KeyWord")) {
            info = new BookInfo();
            info.setBookName(isNull(attrs, "keyWordname"));
            info.setBookId(""); // 为了保存本地数据不报空指针错误
            info.setBookCoverImgUrl(""); // 为了保存本地数据不报空指针错误
            labelList.add(info);
        } else if (localName.equals("Book")) {
            info = new BookInfo();
            this.info.setBookId(isNull(attrs, "bookId"));
            this.info.setBookCoverImgUrl(isNull(attrs, "imgUrl"));
            this.info.setSourceId("");
            booksList.add(info);
        } else if (localName.equals("BookName")) {
            this.inWhich = BOOKNAME;
        } else if (localName.equals("Author")) {
            this.inWhich = AUTHOR;
        } else if (localName.equals("BookType")) {
            this.inWhich = BOOKTYPE;
        } else if (localName.equals("Description")) {
            this.inWhich = BOOKDESCRIPTION;
        } else {
            this.inWhich = 0;
        }

    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}
